package app.blog.entity;

import slap.berkeleydb.BDB;
import app.blog.Blog;

import com.sleepycat.persist.PrimaryIndex;
import com.sleepycat.persist.model.Entity;
import com.sleepycat.persist.model.PrimaryKey;


@Entity
public class Option {
	
	private static PrimaryIndex<String, Option> primaryIndex = 
		BDB.instanse(Blog.bdbName).getEntityStore().getPrimaryIndex(String.class, Option.class);
	
	public static String getOptionValue(String name){
		return getOptionValue(name, String.class);
	}
	
	public static <T> T getOptionValue(String name, Class<T> clazz){
		Option option = primaryIndex.get(name);
		return (T) (option==null ? null : option.value);
	}
	
	public static void persist(String name, Object value){
		persist(new Option(name, value));
	}
	
	public static void persist(Option option){
		primaryIndex.put(option);
	}
	
	private Option() {
	}

	public Option(String name, Object value) {
		this.name = name;
		this.value = value;
	}

	@PrimaryKey
	public String name;
	
	public Object value;
	
}
